package leadingIn;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import theBeans.*;
import theDaoFactory.DaoFactory;
import theDaos.IStudentDao;

/**
 * Servlet implementation class initializeServlet
 */
@WebServlet("/initializeServlet")
public class initializeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public initializeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		List<student> initialList=(ArrayList<student>)request.getAttribute("initialList");
		IStudentDao studentDaoInstance=DaoFactory.getStudentDaoInstance();
		
		for(int i=0,result=0;i<initialList.size();i++)
		{
			student initialOption=initialList.get(i);
			
			String sid=String.valueOf(initialOption.getSid());
			String schoolNum=String.valueOf(initialOption.getSchoolNum());
			String credit=String.valueOf(initialOption.getCredit());
			String ifConfirm=String.valueOf(initialOption.getIfConfirm());
			
			result=studentDaoInstance.initialize(sid, schoolNum, credit, ifConfirm);
			if(result!=1)
			{
				break;
			}
			System.out.println(result);
		}
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
